<?php
class omeanalysts_ome_goodsale extends ectools_analysis_abstract implements ectools_analysis_interface{
	
	public $detail_options = array(
        'hidden' => true,
    );
    public $graph_options = array(
        'hidden' => true,
    );
    public $type_options = array(
        'display' => 'true',
    );
    
    function __construct(&$app) 
    {
        parent::__construct($app);
        $this->_render = kernel::single('desktop_controller');
        
        for($i=0;$i<=5;$i++){
            if ($i == 1) continue;
            $val = $i+1;
            $this->_render->pagedata['time_shortcut'][$i] = $val;
        }

        #仓库
        $branch_list = &app::get('ome')->model('branch')->getList('branch_id,name','',0,-1);
        if ($branch_list){
            foreach ($branch_list as $branch){
                $branch_data[$branch['branch_id']] = $branch['name'];
            }
        }
        $this->_render->pagedata['branch_data'] = $branch_data;
        $this->_render->pagedata['branch_selected'] = $_POST['branch_id'];


        #销售店铺
        $shop_list = &app::get('ome')->model('shop')->getList('shop_id,name','',0,-1);
        if ($shop_list){
            foreach ($shop_list as $type){
                $shops[$type['shop_id']] = $type['name'];
            }
        }
        $this->_render->pagedata['shop_list'] = $shops;
        $this->_render->pagedata['shop_selected'] = $_POST['shop_id'];

        #商品类型
        $goods_type_list = &app::get('ome')->model('goods_type')->getList('type_id,name','',0,-1);
        if ($goods_type_list){
            foreach ($goods_type_list as $type){
                $type_id[$type['type_id']] = $type['name'];
            }
        }
        $this->_render->pagedata['goods_type'] = $type_id;
        $this->_render->pagedata['goodstype_selected'] = $_POST['type_id'];

        #商品品牌
        $goods_brand_list = &app::get('ome')->model('brand')->getList('brand_id,brand_name','',0,-1);
        if ($goods_brand_list){
            foreach ($goods_brand_list as $type){
                $brand_id[$type['brand_id']] = $type['brand_name'];
            }
        }
        $this->_render->pagedata['goods_brand'] = $brand_id;
        $this->_render->pagedata['goodsbrand_selected'] = $_POST['brand_id'];

        $this->_extra_view = array('omeanalysts' => 'ome/goodsale_header.html');
    }
	
    public function finder(){
        $this->export_href = 'index.php?app=omeio&ctl=admin_task&act=create_export&_params[app]=omeanalysts&_params[mdl]=ome_goodsale&_params[time_from]='.$this->_params['time_from'].'&_params[time_to]='.$this->_params['time_to'].'&_params[shop_id]='.$this->_params['shop_id'].'&_params[brand_id]='.$this->_params['brand_id'].'&_params[type_id]='.$this->_params['type_id'].'&_params[branch_id]='.$this->_params['branch_id'];
       return array(
            'model' => 'omeanalysts_mdl_ome_goodsale',
            'params' => array(
                'actions'=>array(
                     array(
                         'label' => '导出',
                         'href' => '#',
                         'target' => "dialog::{width:400,height:170,title:'导出'}",
                         'javascript'=>$this->export_href(),
                         'id'=>'export_id',
                     ),
                ),
                'title'=>app::get('omeanalysts')->_('商品销售汇总<script>if($$(".finder-list").getElement("tbody").get("html") == "\n" || $$(".finder-list").getElement("tbody").get("html") == "" ){$$(".export").set("href","javascript:;").set("onclick", "alert(\"没有可以生成的数据\")");}else{$$(".export").set("href",\'index.php?app=omeanalysts&ctl=ome_goodsale&act=index&action=export\');}</script>'),
                'use_buildin_recycle'=>false,
                'use_buildin_selectrow'=>false,
                'use_buildin_filter'=>false,
            ),
        );
    }
    
    function export_href()
    {
        $base_href = $this->export_href;
        $str = <<<EOF
        <script>
        $('export_id').href="{$base_href}";
        $('export_id').addEvent('click',function(){var finder_id = $('workground').getElement('input[name^=_finder[finder_id]');
        var filter_input = $('finder-filter-'+finder_id.value);
        if(filter_input){
            $('export_id').href="{$base_href}"+"&"+filter_input.value+"&export_href=true";
        }
        });
        </script>
EOF;
        return $str;
    }
}